import {
  getStockPer,
  getRYUpload,
  ryupload,
  uploadBill,
  getBillGrade,
  getHighPremiumsList,
  getBiologicalScienceList,
  getFundFourTradeList,
  getFundTwoList,
  getFundFourList,
  getFundTwoTradeList,
  getBillRiskManage,
  getIndustryPer,
} from '../../../../services/fundposition_service';
import { getTextBoxList } from '../../../../services/option_service';

export default {
  namespace: 'fund',
  state: {
    dataFundtwo: {
      list: [],
      pagination: {},
    },
    dataFundfour: {
      list: [],
      pagination: {},
    },
    dataFundBio: {
      list: [],
      pagination: {},
    },
    data: {
      list: [],
      pagination: {},
    },
    data2: {
      list: {},
      pagination: {},
    },
    filterData: {
      list: [],
    },

    billdata: {
      code: 0,
      list: [],
      pagination: {},
    },

    billriskdata: {
      code: 0,
      list: [],
      pagination: {},
    },

    uploaddata: {
      code: 0,
      data: [],
      message: {},
    },

    industryPerdata: {
      code: '',
      message: '',
      list: {
        title: '',
        date: '',
        items: [],
      },
    },

    stockPerdata: {
      code: 0,
      list: {
        allStockCalValue: 0,
        allStockValue: 0,
        billStockValuePerModels: [],
      },
      pagination: {},
    },
  },

  effects: {
    // 获取 RCN 或者 Twin票据列表
    *GetRYUpload({ payload, callback }, { call, put }) {
      const response = yield call(getRYUpload, payload);
      if (typeof response === 'undefined') {
        return;
      }
      const res = response;
      yield put({
        type: 'rylist',
        payload: res,
      });
      if (callback) callback(res);
    },

    // 获取 票据风控
    *RYUpload({ payload, callback }, { call, put }) {
      const response = yield call(ryupload, payload);
      if (typeof response === 'undefined') {
        return;
      }
      const res = response;
      yield put({
        type: 'uploadry',
        payload: res,
      });
      if (callback) callback(res);
    },

    *GetTextBoxList({ payload, callback }, { call, put }) {
      const response = yield call(getTextBoxList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveFilter',
        payload: response,
      });
      if (callback) callback(response);
    },

    // 获取 票据风控
    *GetBillRiskManage({ payload, callback }, { call, put }) {
      const response = yield call(getBillRiskManage, payload);
      if (typeof response === 'undefined') {
        return;
      }
      const res = response;
      yield put({
        type: 'savebillriskcontrol',
        payload: res,
      });
      if (callback) callback(res);
    },

    // 获取 票据列表
    *GetBillGrade({ payload, callback }, { call, put }) {
      const response = yield call(getBillGrade, payload);
      if (typeof response === 'undefined') {
        return;
      }
      const res = response;

      yield put({
        type: 'savebill',
        payload: res,
      });
      if (callback) callback(res);
    },

    // 获取 生物科技
    *GetBiologicalScienceList({ payload, callback }, { call, put }) {
      const response = yield call(getBiologicalScienceList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (response.total > 0) {
        let stockNumber = 0;
        let thisIncome = 0.0;
        let allIncome = 0.0;
        let allshizhi = 0.0;
        let allPremium = 0.0;

        for (let i = 0; i < response.total; i++) {
          stockNumber += response.rows[i].stocknumber;
          thisIncome = parseFloat(response.rows[i].thisincome) + thisIncome;
          allIncome = parseFloat(response.rows[i].allincome) + allIncome;
          allPremium = parseFloat(response.rows[i].allpremium) + allPremium;
          response.rows[i].shizhi = response.rows[i].costprice * response.rows[i].stocknumber;
          allshizhi += response.rows[i].shizhi;
        }
        response.rows.splice(0, 0, {
          stock: '总收益',
          stocknumber: stockNumber,
          thisincome: thisIncome.toFixed(2),
          allincome: allIncome.toFixed(2),
          allpremium: allPremium.toFixed(2),
          shizhi: allshizhi,
          id: 10000001,
        });
      }
      yield put({
        type: 'saveFundBio',
        payload: response,
      });
      if (callback) callback(response);
    },
    // 高权力金
    *GetHighPremiumsList({ payload, callback }, { call, put }) {
      const response = yield call(getHighPremiumsList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback(response);
    },
    // 二号期权
    *GetFundTwoList({ payload, callback }, { call, put }) {
      const response = yield call(getFundTwoList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback(response);
    },
    // 肆号期权
    *GetFundFourList({ payload, callback }, { call, put }) {
      const response = yield call(getFundFourList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback(response);
    },

    // 获取 二号股票列表
    *GetFundTwoTradeList({ payload, callback }, { call, put }) {
      const response = yield call(getFundTwoTradeList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (response.total > 0) {
        let stockNumber = 0;
        let thisIncome = 0.0;
        let allIncome = 0.0;
        let allshizhi = 0.0;
        let allPremium = 0.0;

        for (let i = 0; i < response.total; i++) {
          stockNumber += response.rows[i].stocknumber;
          thisIncome = parseFloat(response.rows[i].thisincome) + thisIncome;
          allIncome = parseFloat(response.rows[i].allincome) + allIncome;
          allPremium = parseFloat(response.rows[i].allpremium) + allPremium;
          response.rows[i].shizhi = response.rows[i].costprice * response.rows[i].stocknumber;
          allshizhi += response.rows[i].shizhi;
        }
        response.rows.splice(0, 0, {
          stock: '总收益',
          stocknumber: stockNumber,
          thisincome: thisIncome.toFixed(2),
          allincome: allIncome.toFixed(2),
          allpremium: allPremium.toFixed(2),
          shizhi: allshizhi,
          id: 10000001,
        });
      }
      yield put({
        type: 'saveFundtwo',
        payload: response,
      });
      if (callback) callback(response);
    },
    // 获取 四号股票列表
    *GetFundFourTradeList({ payload, callback }, { call, put }) {
      const response = yield call(getFundFourTradeList, payload);
      if (typeof response === 'undefined') {
        return;
      }

      if (response.total > 0) {
        let stockNumber = 0;
        let thisIncome = 0.0;
        let allIncome = 0.0;
        let allshizhi = 0.0;
        let allPremium = 0.0;

        for (let i = 0; i < response.total; i++) {
          stockNumber += response.rows[i].stocknumber;
          thisIncome = parseFloat(response.rows[i].thisincome) + thisIncome;
          allIncome = parseFloat(response.rows[i].allincome) + allIncome;
          allPremium = parseFloat(response.rows[i].allpremium) + allPremium;
          response.rows[i].shizhi = response.rows[i].costprice * response.rows[i].stocknumber;
          allshizhi += response.rows[i].shizhi;
        }
        response.rows.splice(0, 0, {
          stock: '总收益',
          stocknumber: stockNumber,
          thisincome: thisIncome.toFixed(2),
          allincome: allIncome.toFixed(2),
          allpremium: allPremium.toFixed(2),
          shizhi: allshizhi,
          id: 10000003,
        });
      }

      yield put({
        type: 'saveFundFour',
        payload: response,
      });
      if (callback) callback(response);
    },

    // 行业占比
    *GetIndustryPer({ payload, callback }, { call, put }) {
      const response = yield call(getIndustryPer, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveIndustryPer',
        payload: response,
      });
      if (callback) callback(response);
    },

    // 股票敞口
    *GetStockPer({ payload, callback }, { call, put }) {
      const response = yield call(getStockPer, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveStockPer',
        payload: response,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    rylist(state, action) {
      return {
        ...state,
        billdata: action.payload,
      };
    },

    uploadry(state, action) {
      return {
        ...state,
        uploaddata: action.payload,
      };
    },

    uploadbill(state, action) {
      return {
        ...state,
        uploaddata: action.payload,
      };
    },

    savebill(state, action) {
      return {
        ...state,
        billdata: action.payload,
      };
    },

    savebillriskcontrol(state, action) {
      return {
        ...state,
        billriskdata: action.payload,
      };
    },

    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },

    saveFundtwo(state, action) {
      return {
        ...state,
        dataFundtwo: action.payload,
      };
    },

    saveFundFour(state, action) {
      return {
        ...state,
        dataFundfour: action.payload,
      };
    },

    saveFundBio(state, action) {
      return {
        ...state,
        dataFundBio: action.payload,
      };
    },

    saveFilter(state, action) {
      return {
        ...state,
        filterData: action.payload,
      };
    },

    saveIndustryPer(state, action) {
      return {
        ...state,
        industryPerdata: action.payload,
      };
    },

    saveStockPer(state, action) {
      return {
        ...state,
        stockPerdata: action.payload,
      };
    },
  },
};
